package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.DepDao;
import com.hrbxlh.util.pageUtil;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：DepActionImpl.java
* Description：部门actionImpl类,负责操作部门数据以及逻辑处理。
* History：
* Date           Author               Desc
* 2017.1.6		  sy                  建立
* -------------------------------------------------------
*/

@Component("depActionImpl")
@Transactional
public class DepActionImpl {
	
	@Resource(name="depDao")
	private DepDao depDao;
	
	/**
	 * @description 获取全部部门信息
	 * @param Map<String, Object> 如果有查询条件，就将条件set到map中，
	 *        如果是数据库存在的字段，key值要与column的值相同。
	 * @return List<Map<String,Object>> (部门集合)
	 * @throws Exception
	 */
	public List<Map<String, Object>> getDepList(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			returnList=depDao.getDepList(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return returnList;
	}
	
	/**
	 * @description 添加部门的方法，需要部门名称，父部门id，参数key与数据库column对应即可。 
	 * @param Map<String,Object> paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public Map<String, Object> addDep(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultFlag = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> depNameList = depDao.getDepByName(paramMap);
			if(depNameList != null && depNameList.size() != 0){
				resultFlag.put("result", false);
				resultFlag.put("failMsg", "部门名称已存在");
			}else{
				depDao.addDep(paramMap);
				resultFlag.put("result", true);
			}
		} catch (Exception e) {
			throw e;
		}
		return resultFlag;
	}
	
	/**
	 * @description 修改部门的方法，需要部门名称，部门id，父部门id，参数key与数据库column对应即可。 
	 * @param Map<String,Object> paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public boolean saveDep(Map<String, Object> paramMap) throws Exception {

		boolean resultFlag = false;
		try {
			depDao.saveDep(paramMap);
			resultFlag = true;
		} catch (Exception e) {
			throw e;
		}
		return resultFlag;
	}
	
	/**
	 * @description 删除部门的方法，传的是每个部门的id。 
	 * @param .List<Integer> paramList 部门id集合
	 * @return map中get：result,true成功，false失败
	 *         如果是失败的，从map中get出errorMsg，是失败信息。
	 * @throws Exception
	 */
	public Map<String, String> delDep(List<String> paramList) throws Exception {
		Map<String, String> resultMap = new HashMap<String, String>(); 
		String failMsg = "";
		boolean resultFlag = false;
		//用于存放参数
		Map<String, Object> useMap = null;
		List<Integer> myParamList = new ArrayList<Integer>();
		try {
			for(int position=0; position < paramList.size(); position++){
				int depId = Integer.parseInt(paramList.get(position));
				useMap = new HashMap<String, Object>();
				useMap.put("depId", depId);
				List<Map<String, Object>> depMessage = depDao.getDepList(useMap);
				//查询是否有人员
				List<Map<String, Object>> childUserList = depDao.getChildUser(depId);					
				if(childUserList != null && childUserList.size()!=0){
					//failMsg = depMessage.get(0).get("depName") + " 部门有员工，不能删除";
					failMsg = "该部门下存在管理员信息该记录无法删除";
					break;  
			    }else{
			    	myParamList.add(depId);
			    }
			}
			//都满足条件，全部删除
			if(myParamList.size() == paramList.size()){
				depDao.delDep(myParamList);
				resultFlag = true;
			}
			resultMap.put("result", String.valueOf(resultFlag));
			resultMap.put("errorMsg", failMsg);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询部门列表的方法，分页。
	 * @param Map<String,Object> paramMap 存放参数，与数据库字段一致
	 * @return Map<String, Object>  depList部门列表，pageNow当前页数，totalPage总页数
	 * @throws Exception
	 */
	public Map<String, Object> getDepListByPage(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取部门信息
			List<Map<String, Object>> depList = depDao.getDepList(paramMap);
			returnMap.put("depList", depList);
			returnMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allDepList = depDao.getDepList(paramMap);
			int totalPage = pageUtil.getTotalPage(allDepList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
}
